extends Node

@onready var sfx: Node = $SFX
@onready var bgm_player: AudioStreamPlayer = $BGMPlayer

func play_sfx(path: String) -> void:
	var player := sfx.get_node(path) as AudioStreamPlayer
	if not player:
		return
	# 这里只播放一次
	player.play()

# 新增的播放循环音效方法
func play_looping_sfx(path: String) -> void:
	var player := sfx.get_node(path) as AudioStreamPlayer
	if not player:
		return
	# 检查是否已经在播放，避免重复启动
	if not player.playing:
		player.play()

# 新增的停止循环音效方法
func stop_looping_sfx(path: String) -> void:
	var player := sfx.get_node(path) as AudioStreamPlayer
	if not player:
		return
	if player.playing:
		player.stop()
		
# 播放指定背景音乐
func play_bgm(stream: AudioStream) -> void:
	if bgm_player.stream == stream and bgm_player.playing:
		return
	bgm_player.stream = stream
	bgm_player.play()

# 为当前节点及所有子节点下添加音效
func setup_ui_sounds(node: Node) -> void:
	var button = node as Button
	if button:
		button.pressed.connect(play_sfx.bind("UIPressed"))
		button.focus_entered.connect(play_sfx.bind("UIFocus"))

	# 递归执行
	for child in node.get_children():
		setup_ui_sounds(child)
